package com.wits.qso.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wits.comet.chat.common.CometCommand;
import com.wits.comet.chat.common.CometDomain;
import com.wits.comet.chat.entity.ChatMember;
import com.wits.comet.chat.service.CometContext;
import com.wits.platform.common.utils.DateUtil;
import com.wits.platform.common.utils.PageUtil;
import com.wits.platform.web.controller.BaseController;
import com.wits.platform.web.mvc.AjaxResult;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.Vm;
import com.wits.qso.service.BtableDaoService;
import com.wits.qso.service.InstructService;
import com.wits.qso.service.StudentService;
import com.wits.qso.service.VmService;

@Controller
@RequestMapping(value = "qso/teach")
public class TeachController extends BaseController {



    @Autowired
	private VmService vmService;
    
	@Autowired
	private BtableDaoService btableDaoService;
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private InstructService instructService;
	
    /**
     * 老师点击开始授课
     * @return
     */
    @RequestMapping(value="/toTeach")
    public String toTeach(){
    	return "qso/teach/vmDialog";
    }
    
    /**
     * 
     * 可选的所有虚拟机
     * @param model
     * @return
     */
    @RequestMapping(value="/getVm")
    @ResponseBody
    public String vmList(Model model){
    	String userId=this.getCurrentUserId();
    	List<Vm> list=new ArrayList<Vm>();
    	Vm vm=vmService.findByUserId(userId);
    	list.add(vm);
    	return PageUtil.toAjaxPageResult(list.size(), list);
    }
    
    @RequestMapping(value="/connectVm/{vmId}")
    public String connectVm(Model model,@PathVariable("vmId")String vmId){
    	Vm vm=vmService.getOne(vmId);
    	model.addAttribute("vm",vm);
    	return "qso/teach/viewer";
    }
	
	/**
	 * 老师开始授课
	 * 
	 * @param req
	 * @param res
	 * @return
	 */
	@RequestMapping(value = "/beganTeach")
	@ResponseBody
	public Object beganTeach(@RequestParam("vmId") String vmId,@RequestParam("msg") String msg) {
		AjaxResult result=null;
		logger.info("开始教学,vmId={},message={}", vmId,msg);
		String userId=this.getCurrentUserId();
		//老师开始授课,开启共享桌面
		ChatMember member=CometContext.getInstance().getMember(userId);
		if(member!=null){
			member.setShareDesktop(true);
		}
		Btable btable=btableDaoService.findByTeaUserIdAndLessonTime(userId, DateUtil.getNow());
		if(btable!=null){
			List<String> list=studentService.findUserIdByClsId(btable.getClaid().getClsId());
			instructService.teachMsg(CometDomain.main, CometCommand.teaching,list,msg,vmId);
		}else{
			//如果当前时间段内没有课程，则提示不在上课时间内
			HashMap<String,String> hm=new HashMap<String,String>();
			hm.put("hasNoLensson","true");
			result=ajaxDoneSuccess(hm);
		}
		return result;
	}

	/**
	 * 老师下课
	 * 
	 * @param req
	 * @param res
	 * @return
	 */
	@RequestMapping(value = "/endTeach")
	@ResponseBody
	public void classOver(@RequestParam("vmId") String vmId,@RequestParam("msg") String msg) {
		logger.info("下课,vmId={},message={}", vmId,msg);
		String userId=this.getCurrentUserId();
		//老师下课,关闭共享桌面
		ChatMember member=CometContext.getInstance().getMember(userId);
		//下课之前是否处于上课状态中
		boolean wasTeaching=false;
		if(member!=null){
			wasTeaching=member.isShareDesktop();
			member.setShareDesktop(false);
		}
		Btable btable=btableDaoService.findByTeaUserIdAndLessonTime(userId, DateUtil.getNow());
		if(btable!=null&&wasTeaching){// 当且仅当处于上课时间段并处于上课状态中时才发送下课消息。
			List<String> list=studentService.findUserIdByClsId(btable.getClaid().getClsId());
			instructService.teachMsg(CometDomain.main, CometCommand.endTeach,list,msg,vmId);
		}
	}
	
 
}
